package com.owen.springboot.smartparkingms.module.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.owen.springboot.smartparkingms.common.model.Result;
import com.owen.springboot.smartparkingms.module.app.entity.ParkStatus;
import com.owen.springboot.smartparkingms.module.app.service.IParkStatusService;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Owen
 * @since 2022-02-25
 */
@RestController
@RequestMapping("/app/parkStatus")
public class ParkStatusController {

    @Resource
    IParkStatusService parkStatusService;

    @RequestMapping("spaceNumber")
    public Result spaceNumber(Long parkId) {
        Map<String, Object> map = new HashMap<>();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("status","空位");
        queryWrapper.eq("park_id",parkId);
        int spaceNumber = parkStatusService.count(queryWrapper);
        map.put("spaceNumber", spaceNumber);
        return Result.ok(map);
    }

    @RequestMapping("listByParkId")
    public Result listByParkId(Long parkId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("park_id",parkId);
        return Result.ok(parkStatusService.list(queryWrapper));
    }

    @RequestMapping("save")
    public Result save(Long id,String plateNumber) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("plate_number",plateNumber);
        ParkStatus ps = parkStatusService.getOne(queryWrapper);
        if(ps!=null){
            UpdateWrapper updateWrapper = new UpdateWrapper();
            updateWrapper.set("status","空位");
            updateWrapper.set("plate_number",null);
            parkStatusService.update(updateWrapper);
        }
        ParkStatus parkStatus = parkStatusService.getById(id);
        parkStatus.setStatus("占用");
        parkStatus.setPlateNumber(plateNumber);
        parkStatusService.saveOrUpdate(parkStatus);
        return Result.ok();
    }

    @RequestMapping("list")
    public Result list(ParkStatus parkStatus) {
        return parkStatusService.listPage(parkStatus);
    }
}
